﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using System.Resources;
using EHTWebsite.Code;
using System.Text;
using System.IO;
using System.Net;
using System.Web.Security;
using EHTWebsiteProject.Code.Entity;
using EHTWebsiteProject.Code.BLL;

namespace EHTWebsiteProject.Administrator
{
    public partial class EHTNgonNguAdmin : System.Web.UI.Page
    {
         public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
        protected void Page_Load(object sender, EventArgs e)
        {                  
            if (!Page.IsPostBack)
            {
                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);
                LoadNgonNgu();
                if (IsVN == true)
                    plNgonNguOther.Visible = false;
                else
                    plNgonNguVN.Visible = false;
            }
        }

         /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {

            lbHelp.Text = rm.GetString("lbHelp",ci);
            lbMacDinh.Text = rm.GetString("lbMacDinh", ci);
            lbManagerNgonNgu.Text = rm.GetString("lbManagerNgonNgu", ci);
        }

        public void LoadNgonNgu()
        {
            if (Session[HangSo.LANGUAGE_CODE].ToString()=="vi-VN")
            {
                rpNgonNgu.DataSource = NgonNguProvider.GetNgonNgu(HangSo.NgonNgu1, false);
                rpNgonNgu.DataBind();
            }
            else
            {
                rpNgonNguOther.DataSource = NgonNguProvider.GetNgonNgu(HangSo.NgonNgu1, false);
                rpNgonNguOther.DataBind();
            }
        }
        protected void ImgMacDinh_Click(object sender, ImageClickEventArgs e)
        {
            if (Session[HangSo.LANGUAGE_CODE].ToString() != "en-us")
            {
                Int32 ID = 0;  
                Int32 CountVN = 0;
                foreach (RepeaterItem rp in rpNgonNgu.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        ID = Int32.Parse(lbDelID.Text);
                        CountVN++;
                    }
                }
                if (CountVN > 1 || CountVN == 0)
                {
                    ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Eorr_Edit);
                }
                else
                {
                    NgonNguProvider.ChangeNgonNgu(ID, HangSo.NgonNgu1);                   
                }
            }
            else
            {
                Int32 IDOther = 0;  
                Int32 CountOther = 0;
                foreach (RepeaterItem rp in rpNgonNguOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        IDOther = Int32.Parse(lbDelID.Text);
                        CountOther++;
                    }
                }
                if (CountOther > 1 || CountOther == 0)
                {
                    ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Eorr_EditOther);
                }
                else
                {
                    NgonNguProvider.ChangeNgonNgu(IDOther, HangSo.NgonNgu1);                    
                }
            }
            Session[HangSo.LANGUAGE_CODE] = NgonNguProvider.GetNgonNgu(HangSo.NgonNgu1, true).Tables[0].Rows[0]["KyHieu"].ToString().Trim();
            Response.Redirect("EHTNgonNguAdmin.aspx");
        }

        protected void rpNgonNgu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lbTrangThai = (Label)e.Item.FindControl("lbTrangThai");
            Image ImgMacDinh = (Image)e.Item.FindControl("ImgMacDinh");
            try
            {
                if (lbTrangThai.Text == "True")
                {
                    ImgMacDinh.ImageUrl = "~/images/EHTgiaodien/icon-32-default.png";
                   
                }

                else
                    ImgMacDinh.ImageUrl = "~/images/EHTgiaodien/notice-alert.png";
            }
            catch
            { }
        }

        protected void rpNgonNguOther_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lbTrangThai = (Label)e.Item.FindControl("lbTrangThai");
            Image ImgMacDinh = (Image)e.Item.FindControl("ImgMacDinh");
            try
            {
                if (lbTrangThai.Text == "True")
                {
                    ImgMacDinh.ImageUrl = "~/images/EHTgiaodien/icon-32-default.png";

                }

                else
                    ImgMacDinh.ImageUrl = "~/images/EHTgiaodien/notice-alert.png";
            }
            catch
            { }
        }
    }
}